package com.hsteach.util;

import com.hsteach.framework.feign.client.xgk.dto.ret.GetPlaceChildrenTreeRet;
import com.hsteach.web.controller.buildingApply.vo.ListRoomsVo;
import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Comparator;

public class BuildingNameComparator implements Comparator<ListRoomsVo> {

    @Override
    public int compare(ListRoomsVo building1, ListRoomsVo building2) {
            String pinyin1 = getString(building1.getBuildingName());
            String pinyin2 = getString(building2.getBuildingName());
            return pinyin1.compareToIgnoreCase(pinyin2);
    }
    // 获取字符串的拼音
    private String getString(String str) {
        StringBuilder pinyinBuilder = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
                if (pinyinArray != null && pinyinArray.length > 0) {
                    pinyinBuilder.append(pinyinArray[0]); // 取第一个拼音作为简单处理
                }
            } else {
                pinyinBuilder.append(c);
            }
        }
        return pinyinBuilder.toString();
    }
}
